<?php
/*DEE_DESC
 $templ=array(
	//Название шаблона в списке ОБЯЗАТЕЛЬНОЕ
	'title'=>'Список новостей',
	//Приложения которые будут выводится дополнительно при выборе данного шаблона
	'attaches'=>array(
		//Простое поле выбора 
		array('type'=>'field','desc'=>'Количество выводимых новостей на страницу','name'=>'count_on_page')
	));
//DEE_DESC_END*/
	$attaches = $view->getAttaches();
	$limit = !empty( $attaches['count_on_page'] ) ? array_shift( $attaches['count_on_page'] ) : 20;
	if( $limit < 1 ) $limit = 3;

	$cur_page = preg_match( '#/page-(\d+)#' , $_SERVER['REQUEST_URI'] , $res ) ? $res[1]-1:0;
	if( $cur_page < 0 ) $cur_page = 0;
	$elog->add( $cur_page );
	$conn->query('
		SELECT sql_calc_found_rows UNIX_TIMESTAMP( `p`.`date_create` ) AS `date_create`, p.url, p.title, p.text1, pa.params
			FROM pages AS p 
				LEFT JOIN pages_attachments AS pa on pa.page_id = p.id AND pa.name = "news_image" 
					WHERE p.templ = "news_item" 
						ORDER BY 1 DESC LIMIT 
						'.( ( $view->paginator_index ) * $limit ).', '.$limit );

	$url_before = $view->url_before.'/'.$view->url;
	$view->gText .= $view->printH1('v');
	if( $conn->rowCount() )
	{
	
		$view->gText.='
					<ul class="global-list">
					';
		while( $row=$conn->fetch() )
		{
			$img = unserialize( $row->params );
			$img = $img ? $load->module('imager','getThumbName',array('original_name'=>'/inc/images/news'.$img[0],'size'=>'g')):'';

			$view->gText.='
						<li>
							<div class="global-photo">
									<img src="'.$img.'" alt="img" title="'.$row->title.'"/>
							</div>

							<div class="global-list__description">
								<h2>
									<a href="'.$url_before.'/'.$row->url.'" title="'.$row->title.'">'.$row->title.'</a>
								</h2>
								<span class="date">'.getDateFormat($row->date_create).'</span>
								<p>'.$row->text1.'</p>
								<a href="'.$url_before.'/'.$row->url.'" title="'.$row->title.'" class="global-button">
									<span class="global-button__inner">Подробнее</span>
								</a>
							</div>
						</li>
						';
		}
		$view->gText.='
					</ul>
					';
	}
	
	$conn->query('select found_rows() as num');
	$total=$conn->fetch();
	if($total)
		$total=$total->num;
	else $total=0;
	$view->gText.=$load->block('pagination',array('total'=>$total,'limit'=>$limit,'url_before'=>$url_before));
	
	